<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/5/19 0019
 * Time: 16:56
 */
namespace App\Admin\Controllers;
use App\Admin\Contracts\MemberInterface;
use App\Base\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Validator;
class ClientController extends Controller
{

    /**
     * 用户接口类
     * @var MemberInterface $iMember
     */
    private $iMember;
    /**
     * 构造函数
     * IndexController constructor.
     */
    public function __construct(){
        $this->iMember=app()->make(MemberInterface::class);
    }
    /**
     * 登陆
     */
    public function login(Request $request){
        if($request->getMethod() == "POST"){
            $validator=Validator::make($request->all(), [
                'username' => 'required',
                'password' => 'required',
            ],["username.required"=>'用户名是必须的',"password.required"=>'密码是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->iMember->login($request->all());
        }
        /**
         * 是否开启Google验证
         */
        $checkShow = get_config_param_value("BACKEND_SWITCH_OTP");
        return view("admin.client.login")->with("checkShow",$checkShow);
    }

    /**
     * 登陆日志
     */
    public function loginLog(){
        return view("admin.client.login_log");
    }

    /**
     * google opt
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function loginOtp(){

        $secret = $this->iMember->getGoogleSecret();
        $adminUsername = session('lqwsys_member_username');  //登陆用户名
        $qrCodeUrl="otpauth://totp/" . $adminUsername . "?secret=" . $secret;//生成二维码
        return view("admin.client.login_otp")->with("secret",$secret)->with("qr_code_url",$qrCodeUrl);
    }

    /**
     * 已扫二维码进行帐号邦定
     * @param Request $request
     * @return mixed
     */
    public function bindGoogleOtp(Request $request){
        if($request->getMethod()=="POST"){

            $validator = Validator::make($request->all(), [
                'secret' => 'required',
            ], ["secret.required" => 'google加密码是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->iMember->bindGoogleOtp($request->all());
        }
    }

    /**
     * 退出登陆
     * @param Request $request
     * @return array
     */
    public function logout(Request $request){
        if (is_login()) {
            session([
                "lqwsys_auth" => null
            ]);
            session([
                "lqwsys_auth_sign" => null
            ]);
            session()->save();
            Cookie::queue('lqwsys_auth_sign', null , -1);
            Cookie::forget('lqwsys_auth_sign');
            return array();
        } else {
            Cookie::queue('lqwsys_auth_sign', null , -1);
            Cookie::forget('lqwsys_auth_sign');
            redirect("admin/client/login");
        }
    }

    /**
     * 忘记密码
     */
    public function forget(){
        return view("admin.public.forget");
    }
}